<?php

class PruebasController extends AppController {

    var $name = 'Pruebas';
    
    function beforeFilter() {
        parent::beforeFilter();

        //si no es un usuario con nivel = 2 o 1 no lo dejo acceder a las siguientes funciones de este controlador
        if (in_array($this->action, array('index', 'grafico1', 'grafico2', 'grafico3', 'grafico4')) and $this->Auth->user('id') and !(in_array($this->Auth->user('level'), array(1,2)))) {
            $this->redirect(array('user' => false, 'controller' => 'articles', 'action' => 'inicio'));
        }
    }

    function index() {
//		$this->Sale->recursive = 0;
//		$this->set('sales', $this->paginate());
    }

    function grafico1() {

        $this->set('mostrar_grafica', 0);

        if (!empty($this->data)) {
            $this->set('mostrar_grafica', 1);
            /* CAT:Line chart */

            /* pChart library inclusions */
            include(SITE_PATH . "/app/vendors/pchart/class/pData.class.php");
            include(SITE_PATH . "/app/vendors/pchart/class/pDraw.class.php");
            include(SITE_PATH . "/app/vendors/pchart/class/pImage.class.php");

            $db = mysql_connect("localhost", "root", "");
            if ($db == "") {
                echo " DB Connection error...\r\n";
                exit();
            }

            mysql_select_db("cerrajeria", $db);
            /* Create and populate the pData object */
            $MyData = new pData();
            //Añadire los datos de la base de datos
            //DATOS DE LAS COMPRAS
            $compras = $this->Prueba->query("SELECT month(purchase_date) as mes, SUM(purchase_amount) as suma FROM `purchases` WHERE year(purchases.purchase_date) = " . $this->data['Prueba']['anio'] . " group by month(purchase_date)");
//         pr($compra[0][0]['mes']);die;
            $array_datos = array();
            foreach ($compras as $comp) {
                $array_datos[$comp[0]['mes']] = $comp[0];
            }
//        pr($compra);pr($array_datos);
            for ($i = 1; $i <= 12; $i++) {
                if (!isset($array_datos[$i])) {
                    $MyData->addPoints(0, "Compras");
                } else {
                    $MyData->addPoints($array_datos[$i]['suma'], "Compras");
                }
            }
            //DATOS DE LAS VENTAS
            $ventas = $this->Prueba->query("SELECT month( sales.sale_date ) AS mes, SUM( items.sale_amount ) AS suma FROM items, sales WHERE items.sale_id = sales.id AND year(sales.sale_date) = " . $this->data['Prueba']['anio'] . " GROUP BY month( sales.sale_date )");
            $array_datos1 = array();
            foreach ($ventas as $vent) {
                $array_datos1[$vent[0]['mes']] = $vent[0];
            }
//        pr($venta);pr($array_datos1);die;
            for ($i = 1; $i <= 12; $i++) {
                if (!isset($array_datos1[$i])) {
                    $MyData->addPoints(0, "Ventas");
                } else {
                    $MyData->addPoints($array_datos1[$i]['suma'], "Ventas");
                }
            }


            $MyData->setAxisName(0, "Cantidad");
            $meses = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
            $MyData->addPoints($meses, "Months");
            $MyData->setSerieDescription("Months", "Month");
            $MyData->setAbscissa("Months");

            $this->set('compras', $compras);
            $this->set('ventas', $ventas);
            $this->set('meses', $meses);
//            pr($meses);pr($compras);pr($ventas);die;

            /* Create the pChart object */
            $myPicture = new pImage(1000, 355, $MyData);

            /* Turn of Antialiasing */
            $myPicture->Antialias = FALSE;

            /* Add a border to the picture */
            $myPicture->drawGradientArea(0, 0, 1000, 355, DIRECTION_VERTICAL, array("StartR" => 240, "StartG" => 240, "StartB" => 240, "EndR" => 180, "EndG" => 180, "EndB" => 180, "Alpha" => 100));
            $myPicture->drawGradientArea(0, 0, 1000, 355, DIRECTION_HORIZONTAL, array("StartR" => 240, "StartG" => 240, "StartB" => 240, "EndR" => 180, "EndG" => 180, "EndB" => 180, "Alpha" => 20));
            $myPicture->drawRectangle(0, 0, 999, 354, array("R" => 0, "G" => 0, "B" => 0));

            /* Set the default font */
            $myPicture->setFontProperties(array("FontName" => SITE_PATH . "/app/vendors/pchart/fonts/pf_arma_five.ttf", "FontSize" => 10));

            /* Define the chart area */
            $myPicture->setGraphArea(60, 40, 950, 300);

            /* Draw the scale */
            $scaleSettings = array("GridR" => 200, "GridG" => 200, "GridB" => 200, "DrawSubTicks" => TRUE, "CycleBackground" => TRUE);
            $myPicture->drawScale($scaleSettings);

            /* Write the chart legend */
            $myPicture->drawLegend(580, 12, array("Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL));

            /* Turn on shadow computing */
            $myPicture->setShadow(TRUE, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));

            /* Draw the chart */
            $myPicture->setShadow(TRUE, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));
            $settings = array("Surrounding" => -30, "InnerSurrounding" => 30, "Interleave" => 0);
            $myPicture->drawBarChart($settings);

            /* Render the picture (choose the best way) */
            $myPicture->render("img/grafico1.png");
        }
        $anios = $this->Prueba->query("SELECT distinct(year(sales.sale_date)) as anio FROM sales WHERE 1 order by year(sales.sale_date) desc;");
//        pr($anios);die;
        if(sizeof($anios) == 0){
            $anios = $this->Prueba->query("SELECT distinct(year(purchases.purchase_date)) as anio FROM purchases WHERE 1 order by year(purchases.purchase_date) desc;");
        }
        $year = array();
        $i = 0;
        foreach ($anios as $anio) {
            $year[$anio[0]['anio']] = $anio[0]['anio'];
        }
        $this->set(compact('year'));
    }

    function grafico2() {
        $this->set('mostrar_grafica', 0);

        if (!empty($this->data)) {
            $this->set('mostrar_grafica', 1);
        $opcion = '';
        if($this->data['Prueba']['retoque'] == 0){
            $opcion = 'Si';
        }else{
            $opcion = 'No';
        }
        /* CAT:Pie charts */
        /* pChart library inclusions */
        include(SITE_PATH . "/app/vendors/pchart/class/pData.class.php");
        include(SITE_PATH . "/app/vendors/pchart/class/pDraw.class.php");
        include(SITE_PATH . "/app/vendors/pchart/class/pPie.class.php");
        include(SITE_PATH . "/app/vendors/pchart/class/pImage.class.php");

        /* Create and populate the pData object */
        $MyData = new pData();
        //Traigo los datos de la base de datos
        if($opcion == "No"){
//            pr($opcion);die;
            $datos = $this->Prueba->query("SELECT types.name as tipo, SUM(items.sale_amount) as suma FROM items, articles, types WHERE items.article_id = articles.id AND articles.type_id = types.id AND items.retouch = '".$opcion."' GROUP BY types.name");
        }else{
//            pr($opcion);die;
            $datos = $this->Prueba->query("SELECT types.name as tipo, SUM(items.sale_amount) as suma FROM items, articles, types WHERE items.article_id = articles.id AND articles.type_id = types.id GROUP BY types.name");
        }
//         pr($datos);die;
        //Aca le paso a la vista el arreglo de los datos en una variable $datos
        $this->set('datos', $datos);

        foreach ($datos as $dato) {
            $MyData->addPoints($dato['types']['tipo'], "Labels");
            $MyData->addPoints($dato[0]['suma'], "ScoreA");
        }
        //Fin insercion de datos
//         $MyData->addPoints(array(10,60,30),"ScoreA");  
        $MyData->setSerieDescription("ScoreA", "Application A");

        /* Define the absissa serie */
//         $MyData->addPoints(array("LLaves","Cerraduras","Candados"),"Labels");
        $MyData->setAbscissa("Labels");

        /* Create the pChart object */
        $myPicture = new pImage(650, 300, $MyData, TRUE);

        /* Add a border to the picture */
        $myPicture->drawGradientArea(0, 0, 650, 300, DIRECTION_VERTICAL, array("StartR" => 240, "StartG" => 240, "StartB" => 240, "EndR" => 180, "EndG" => 180, "EndB" => 180, "Alpha" => 100));
        $myPicture->drawGradientArea(0, 0, 650, 300, DIRECTION_HORIZONTAL, array("StartR" => 240, "StartG" => 240, "StartB" => 240, "EndR" => 180, "EndG" => 180, "EndB" => 180, "Alpha" => 20));
        $myPicture->drawRectangle(0, 0, 649, 299, array("R" => 0, "G" => 0, "B" => 0));

        /* Set the default font properties */
        $myPicture->setFontProperties(array("FontName" => SITE_PATH . "/app/vendors/pchart/fonts/pf_arma_five.ttf", "FontSize" => 10, "R" => 10, "G" => 10, "B" => 10));

        /* Create the pPie object */
        $PieChart = new pPie($myPicture, $MyData);

        /* Enable shadow computing */
        $myPicture->setShadow(TRUE, array("X" => 3, "Y" => 3, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));

        /* Draw a splitted pie chart */
//         $PieChart->draw3DPie(180,135,array("Radius"=>160,"DataGapAngle"=>12,"DataGapRadius"=>10,"Border"=>TRUE));

        /* Draw an AA pie chart */
        $PieChart->draw3DPie(310, 165, array("WriteValues" => TRUE, "ValueR" => 10, "ValueG" => 10, "ValueB" => 10, "Radius" => 150, "DrawLabels" => TRUE, "LabelStacked" => TRUE, "Border" => TRUE));

        /* Write the legend box */
//         $myPicture->setFontProperties(array("FontName"=>SITE_PATH."/app/vendors/pchart/fonts/pf_arma_five.ttf","FontSize"=>10,"R"=>0,"G"=>0,"B"=>0));
//         $PieChart->drawPieLegend(30,240,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));

        /* Render the picture (choose the best way) */
        $myPicture->render("img/grafico2.png");
        }
    }

    function grafico3() {

        $this->set('mostrar_grafica', 0);

        if (!empty($this->data)) {
            $this->set('mostrar_grafica', 1);
            /* CAT:Line chart */

            /* pChart library inclusions */
            include(SITE_PATH . "/app/vendors/pchart/class/pData.class.php");
            include(SITE_PATH . "/app/vendors/pchart/class/pDraw.class.php");
            include(SITE_PATH . "/app/vendors/pchart/class/pImage.class.php");

            $db = mysql_connect("localhost", "root", "");
            if ($db == "") {
                echo " DB Connection error...\r\n";
                exit();
            }

            mysql_select_db("cerrajeria", $db);
            /* Create and populate the pData object */
            $MyData = new pData();
            //Añadire los datos de la base de datos
            //DATOS DE LAS COMPRAS
            $compras = $this->Prueba->query("SELECT month(sales.sale_date) as mes, SUM(sales.total) as suma FROM sales WHERE year(sales.sale_date) = " . $this->data['Prueba']['anio'] . " group by month(sales.sale_date)");
//         pr($compra[0][0]['mes']);die;
            $array_datos = array();
            foreach ($compras as $comp) {
                $array_datos[$comp[0]['mes']] = $comp[0];
            }
//        pr($compra);pr($array_datos);
            for ($i = 1; $i <= 12; $i++) {
                if (!isset($array_datos[$i])) {
                    $MyData->addPoints(0, "Compras");
                } else {
                    $MyData->addPoints($array_datos[$i]['suma'], "Compras");
                }
            }
            //DATOS DE LAS VENTAS
            $ventas = $this->Prueba->query("SELECT month(purchases.purchase_date) as mes, articles.price*purchases.purchase_amount as suma FROM purchases, articles WHERE purchases.article_id = articles.id AND year(purchases.purchase_date) = " . $this->data['Prueba']['anio'] . " group by month(purchases.purchase_date)");
            $array_datos1 = array();
            foreach ($ventas as $vent) {
                $array_datos1[$vent[0]['mes']] = $vent[0];
            }
//        pr($venta);pr($array_datos1);die;
            for ($i = 1; $i <= 12; $i++) {
                if (!isset($array_datos1[$i])) {
                    $MyData->addPoints(0, "Ventas");
                } else {
                    $MyData->addPoints($array_datos1[$i]['suma'], "Ventas");
                }
            }


            $MyData->setAxisName(0, "Cantidad");
            $meses = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
            $MyData->addPoints($meses, "Months");
            $MyData->setSerieDescription("Months", "Month");
            $MyData->setAbscissa("Months");

            $this->set('compras', $compras);
            $this->set('ventas', $ventas);
            $this->set('meses', $meses);
//            pr($meses);pr($compras);pr($ventas);die;

            /* Create the pChart object */
            $myPicture = new pImage(1000, 355, $MyData);

            /* Turn of Antialiasing */
            $myPicture->Antialias = FALSE;

            /* Add a border to the picture */
            $myPicture->drawGradientArea(0, 0, 1000, 355, DIRECTION_VERTICAL, array("StartR" => 240, "StartG" => 240, "StartB" => 240, "EndR" => 180, "EndG" => 180, "EndB" => 180, "Alpha" => 100));
            $myPicture->drawGradientArea(0, 0, 1000, 355, DIRECTION_HORIZONTAL, array("StartR" => 240, "StartG" => 240, "StartB" => 240, "EndR" => 180, "EndG" => 180, "EndB" => 180, "Alpha" => 20));
            $myPicture->drawRectangle(0, 0, 999, 354, array("R" => 0, "G" => 0, "B" => 0));

            /* Set the default font */
            $myPicture->setFontProperties(array("FontName" => SITE_PATH . "/app/vendors/pchart/fonts/pf_arma_five.ttf", "FontSize" => 10));

            /* Define the chart area */
            $myPicture->setGraphArea(60, 40, 950, 300);

            /* Draw the scale */
            $scaleSettings = array("GridR" => 200, "GridG" => 200, "GridB" => 200, "DrawSubTicks" => TRUE, "CycleBackground" => TRUE);
            $myPicture->drawScale($scaleSettings);

            /* Write the chart legend */
            $myPicture->drawLegend(580, 12, array("Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL));

            /* Turn on shadow computing */
            $myPicture->setShadow(TRUE, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));

            /* Draw the chart */
            $myPicture->setShadow(TRUE, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));
            $settings = array("Surrounding" => -30, "InnerSurrounding" => 30, "Interleave" => 0);
            $myPicture->drawBarChart($settings);

            /* Render the picture (choose the best way) */
            $myPicture->render("img/grafico3.png");
        }
        $anios = $this->Prueba->query("SELECT distinct(year(sales.sale_date)) as anio FROM sales WHERE 1 order by year(sales.sale_date) desc;");
//        pr($anios);die;
        if(sizeof($anios) == 0){
            $anios = $this->Prueba->query("SELECT distinct(year(purchases.purchase_date)) as anio FROM purchases WHERE 1 order by year(purchases.purchase_date) desc;");
        }
        $year = array();
        $i = 0;
        foreach ($anios as $anio) {
            $year[$anio[0]['anio']] = $anio[0]['anio'];
        }
        $this->set(compact('year'));
    }

    //SELECT month(sales.sale_date) as mes, SUM(items.sale_amount) as suma FROM items, sales WHERE items.sale_id = sales.id AND year(sales.sale_date) =  " . $this->data['Prueba']['anio'] . " AND items.retouch = 'Si' GROUP BY month(sales.sale_date)
    function grafico4() {
        $this->set('mostrar_grafica', 0);

        if (!empty($this->data)) {
            $this->set('mostrar_grafica', 1);
            /* CAT:Pie charts */
            /* pChart library inclusions */
            include(SITE_PATH . "/app/vendors/pchart/class/pData.class.php");
            include(SITE_PATH . "/app/vendors/pchart/class/pDraw.class.php");
            include(SITE_PATH . "/app/vendors/pchart/class/pPie.class.php");
            include(SITE_PATH . "/app/vendors/pchart/class/pImage.class.php");

            /* Create and populate the pData object */
            $MyData = new pData();
            //Traigo los datos de la base de datos
            $datos = $this->Prueba->query("SELECT month(sales.sale_date) as mes, SUM(items.sale_amount) as suma FROM items, sales WHERE items.sale_id = sales.id AND year(sales.sale_date) =  " . $this->data['Prueba']['anio'] . " AND items.retouch = 'Si' GROUP BY month(sales.sale_date)");
//         pr($datos);die;
            $array_datos1 = array();
            foreach ($datos as $dato) {
                $array_datos1[$dato[0]['mes']] = $dato[0];
            }
//        pr($venta);pr($array_datos1);die;
            for ($i = 1; $i <= 12; $i++) {
                if (!isset($array_datos1[$i])) {
                    $MyData->addPoints(0, "ScoreA");
                } else {
                    $MyData->addPoints($array_datos1[$i]['suma'], "ScoreA");
                }
            }

//            foreach ($datos as $dato) {
////                $MyData->addPoints($dato['types']['tipo'], "Labels");
//                $MyData->addPoints($dato[0]['suma'], "ScoreA");
//            }
            //Fin insercion de datos
//         $MyData->addPoints(array(10,60,30),"ScoreA");  
            $MyData->setSerieDescription("ScoreA", "Application A");

            /* Define the absissa serie */
            $meses = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
            $MyData->addPoints($meses,"Labels");
            $MyData->setAbscissa("Labels");
            
            //Aca le paso a la vista el arreglo de los datos en una variable $datos
            $this->set('datos', $datos);
            $this->set('meses', $meses);

            /* Create the pChart object */
            $myPicture = new pImage(650, 300, $MyData, TRUE);

            /* Add a border to the picture */
            $myPicture->drawGradientArea(0, 0, 650, 300, DIRECTION_VERTICAL, array("StartR" => 240, "StartG" => 240, "StartB" => 240, "EndR" => 180, "EndG" => 180, "EndB" => 180, "Alpha" => 100));
            $myPicture->drawGradientArea(0, 0, 650, 300, DIRECTION_HORIZONTAL, array("StartR" => 240, "StartG" => 240, "StartB" => 240, "EndR" => 180, "EndG" => 180, "EndB" => 180, "Alpha" => 20));
            $myPicture->drawRectangle(0, 0, 649, 299, array("R" => 0, "G" => 0, "B" => 0));

            /* Set the default font properties */
            $myPicture->setFontProperties(array("FontName" => SITE_PATH . "/app/vendors/pchart/fonts/pf_arma_five.ttf", "FontSize" => 10, "R" => 10, "G" => 10, "B" => 10));

            /* Create the pPie object */
            $PieChart = new pPie($myPicture, $MyData);

            /* Enable shadow computing */
            $myPicture->setShadow(TRUE, array("X" => 3, "Y" => 3, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));

            /* Draw a splitted pie chart */
//         $PieChart->draw3DPie(180,135,array("Radius"=>160,"DataGapAngle"=>12,"DataGapRadius"=>10,"Border"=>TRUE));

            /* Draw an AA pie chart */
            $PieChart->draw3DPie(310, 165, array("WriteValues" => TRUE, "ValueR" => 10, "ValueG" => 10, "ValueB" => 10, "Radius" => 150, "DrawLabels" => TRUE, "LabelStacked" => TRUE, "Border" => TRUE));

            /* Write the legend box */
//         $myPicture->setFontProperties(array("FontName"=>SITE_PATH."/app/vendors/pchart/fonts/pf_arma_five.ttf","FontSize"=>10,"R"=>0,"G"=>0,"B"=>0));
//         $PieChart->drawPieLegend(30,240,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));

            /* Render the picture (choose the best way) */
            $myPicture->render("img/grafico4.png");
        }
        $anios = $this->Prueba->query("SELECT distinct(year(sales.sale_date)) as anio FROM sales WHERE 1 order by year(sales.sale_date) desc;");
//        pr($anios);die;
        $year = array();
        $i = 0;
        foreach ($anios as $anio) {
            $year[$anio[0]['anio']] = $anio[0]['anio'];
        }
        $this->set(compact('year'));
    }

}
